/*
 * Copyright (C) 2008,2009,2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

#ifndef _erecord_h
#define _erecord_h

#include "etypes.h"

#define ebasic_p(object)	(etype(object) == t_basic)
#define estruct_p(object)	(etype(object) == t_struct)
#define eclass_p(object)	(etype(object) == t_class)
#define eunion_p(object)	(etype(object) == t_union)

/*
 * Types
 */
struct erecord {
    /***********************************
     * struct, union and class fields
     ***********************************/
    esymbol_t	*name;

    /* hash table of record fields */
    ehash_t	*fields;

    /* sorted vector of record fields */
    evector_t	*vfield;

    /***********************************
     * struct and class fields
     ***********************************/
    /* type indentifier */
    etype_t	 type;

    /* size of the record type */
    eint32_t	 size;

    /* offsets that should be garbage collected */
    evector_t	*offsets;

    /* FIXME may be moved to class only fields
     * but delay a bit, as it may be useful in
     * a few cases to know about an object that
     * is fully 'binary' and 'syntax' compatible */
    /* first direct superclass */
    erecord_t	*super;

    /***********************************
     * class fields
     ***********************************/
    /* methods information */
    ehash_t	*methods;

    /* sorted vector of class methods */
    evector_t	*vmethod;
};

/*
 * Prototypes
 */
extern esymbol_t *
eget_field(erecord_t *record, evector_t *name);

extern esymbol_t *
enew_field(erecord_t *record, evector_t *name, etag_t *tag);

extern erecord_t *
eadd_record(erecord_t *record, erecord_t *super);

extern erecord_t *
enew_basic(esymbol_t *name, eint32_t size);

extern erecord_t *
enew_record(esymbol_t *name, etype_t type);

/*   Allocate a type, but doesn't finish the record definition.
 *   The record type initially has the value 't_pointer',
 * to mean it may be a declaration of a never defined type,
 * but once definition starts, this function can be called
 * to allocate the type.
 *   If 'record->type' has the value 't_pointer', edef_record
 * allocates a new type, and sets 'record->type' to 't_pointer & type'
 * where 'type' will the final record type, and can temporarily be
 * read as 'record->type & ~t_pointer'.
 *   If the record type has been already allocated, this function
 * is a noop.
 */
extern void
edef_record(erecord_t *record);

extern erecord_t *
eend_record(erecord_t *record);

#endif /* _erecord_h */
